package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.app.Dialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainUdis;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.util.LinkedHashMap;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：flyinbed on 2017/11/6 18:22
 * 邮箱：zhanghuaiha@gmail.com
 */

public class PaperBHActivity extends BaseActivity {
    private EditText tihuan,leijia;
    private ImageView back;
    private TextView save;
    private String alias;
    private Dialog progressDialog;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_paper_bh;
    }

    @Override
    public void initView() {
        tihuan = (EditText) findViewById(R.id.bh_tihuan);
        leijia = (EditText) findViewById(R.id.bh_leijia);
        back = (ImageView) findViewById(R.id.bh_back);
        save = (TextView) findViewById(R.id.bh_save);
    }

    @Override
    public void initData() {
        alias = getIntent().getStringExtra("alias");
    }

    @Override
    public void initListener() {
        back.setOnClickListener(view -> finish());
        save.setOnClickListener(view -> {
            dialog();
            String th = tihuan.getText().toString().trim();
            String lj = leijia.getText().toString().trim();
            if (TextUtils.isEmpty(th) && TextUtils.isEmpty(lj)) {
                ToastUtil.showToast("数据不能为空");
                progressDialog.dismiss();
                return;
            }
            net(th,lj);

        });
        tihuan.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().length() > 0) {
                    leijia.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        leijia.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().length() > 0) {
                    tihuan.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public void net(String ti,String lei){
        String time = System.currentTimeMillis() + "";
        String udid = ObtainUdis.getUDID(PaperBHActivity.class);
        String versionName = ObtainVersion.getVersionName(this);
        String eid = App.SP.getString("id", "");
        Map<String,String> map = new LinkedHashMap<>();
        map.put("rf","2");
        map.put("ts",time);
        map.put("v",versionName);
        map.put("i", NetWorkCodeInfo.ACTIVATION);
        map.put("udid",udid);
        map.put("eid",eid);
        map.put("alias",alias);
        if (!TextUtils.isEmpty(ti) || !"".equals(ti)) {
            map.put("count",ti);
            map.put("type","1");
        }

        if (!TextUtils.isEmpty(lei) || !"".equals(lei)) {
            map.put("count",lei);
            map.put("type","2");
        }


        String encry1 = ObtainEncry.getEncry(map);
        map.put("encry",encry1);

        RetrofitFactory.getmInstanse().mApi.UploadPaper(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity>() {
                    @Override
                    public void success(BaseResponseEntity baseResponseEntity) {
                        if (baseResponseEntity.code == 200) {
                            ToastUtil.showToast("补纸成功");
                            finish();
                        }else {
                            ToastUtil.showToast(baseResponseEntity.message);
                        }
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        progressDialog.dismiss();
                    }
                });
    }
    public void dialog(){
        progressDialog = new Dialog(this,R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog);
        progressDialog.setCancelable(true);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        msg.setText("正在加载中.");
        progressDialog.show();
    }
}
